
import Layout from '@/components/Layout/index.vue'

/*本地路由和服务器路由切换开关，true打开服务器路由*/
export const USE_REMOTE_ROUTE_TABLE = true

// 递归路由懒加载
export function dyncRoutes(routes: Array<any>): Array<any> {
  return traveralRoute(routes, process.env.NODE_ENV === 'production' ? process.env.BASE_URL : '/')
  function traveralRoute(routes: Array<any>, parentPath: string | undefined) {
    for (let i = 0, l = routes.length; i < l; i++) {
      const route = routes[i]
      
      if (route.component === 'Layout') {
        route.component = () => import('@/components/Layout/index.vue')
      } else {
        const component: string = route.component
        route.component = () => import(`@/views/${component}/index.vue`)
      }

      if (route.path !== '/') {
        route.path =
          (parentPath?.endsWith('/') ? parentPath : parentPath + '/') + (route.path || '')
      }
      // console.log(route.path)
      if (route.children != null && route.children && route.children.length) {
        route.children = traveralRoute(route.children, route.path)
      }
    }
    return routes
  }
}
export function filterAsyncRouter(asyncRouterMap, parentPath = '') {
  console.log(asyncRouterMap,'开始filterAsyncRouter');
  return asyncRouterMap.filter(route => {
    console.log(route.path,'route.path1');
    route.path = parentPath + '/' + route.path
    console.log(route.path,'route.path2');

    if (route.component) {
      // Layout组件特殊处理
      if (route.component === 'Layout') {
        route.component = Layout
      } else {
          // route.component = loadView(route)
          // route.component = () => import(`@/views/${route.component}`)
          const component: string = route.component
          route.component = () => import(`@/views/${component}/index.vue`)
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = dynamicRoutes(filterAsyncRouter(route.children, route.path))
    }
    // if(route.ip && route.ip.startsWith("//") ){
    //   route.name =  route.ip
    //   route.component = () => import('@/components/OutlinkView/index.vue')
    // }
    // if (route.name === '参数库管理') {
    //   // route.path = '/project-market-development/parametermgt'
    //   route.component = () => import('@/components/MicroApp.vue')
    // }
    // console.log("route.children",route.children);
    return true
  })
}
// export const loadView = view => {
//   // 路由懒加载
//   // if (ip) { return () => import('@/components/MicroApp.vue') } // 微应用
//   // return () => import('@/components/MicroApp.vue')
//   const component=view.component
//   return () => import(`@/views/${component}`)
// }
// 递归路由懒加载
export function dynamicRoutes(routes) {
  return traveralRoute(routes, process.env.NODE_ENV === "production" ? process.env.BASE_URL : "/");

  function traveralRoute(routes, parentPath) {
      for (let i = 0, l = routes.length; i < l; i++) {
          const route = routes[i];
          // const component = route.component;
          // route.component = () => import(`@/views/${component}/index.vue`);
          // console.log("子：" + route.path)
          // console.log("父：" + parentPath)

          route.path = removeMoreSlash(parentPath, route.path)
          if (route.redirect != undefined) {
              route.redirect = removeMoreSlash(process.env.NODE_ENV === "production" ? process.env.BASE_URL : "/", route.redirect)
          }
          // console.log(route.path)
          if (route.children != null && route.children && route.children.length) {
              route.children = traveralRoute(route.children, route.path);
          }
      }
      // console.log(routes);
      return routes;
  }
}
function  removeMoreSlash(parentPath ,path) {
  if (path.startsWith("/") && parentPath.endsWith("/")) {
      path = path.substring(1);
  } else if (!path.startsWith("/") && !parentPath.endsWith("/")) {
      path = path + "/";
  }
  return  parentPath + (path || "");
}